<template>
  <div class="app-container">
    <div style="display: flex;justify-content: space-between">
      <el-button size="small" icon="el-icon-plus" type="primary" @click="handleCreate">添加工资账套</el-button>
      <el-button size="small" icon="el-icon-refresh" type="success" @click="handleRefresh" />
    </div>
    <div style="margin-top: 10px">
      <el-table :data="list" size="small" border stripe>
        <el-table-column type="selection" width="40" />
        <el-table-column prop="name" label="账套名称" width="120" align="center" />
        <el-table-column prop="basicSalary" label="基本工资" width="80" align="center" />
        <el-table-column prop="bonus" label="奖金" width="80" align="center" />
        <el-table-column prop="lunchSalary" label="午餐补助" width="80" align="center" />
        <el-table-column prop="trafficSalary" label="交通补助" width="80" align="center" />
        <el-table-column prop="allSalary" label="应发工资" width="80" align="center" />
        <el-table-column label="养老金" align="center">
          <el-table-column prop="pensionBase" label="基数" width="60" align="center" />
          <el-table-column prop="pensionPer" label="比率" width="60" align="center" />
        </el-table-column>
        <el-table-column prop="createDate" label="启用时间" width="100" align="center" />
        <el-table-column label="医疗保险" align="center">
          <el-table-column prop="medicalBase" label="基数" width="60" align="center" />
          <el-table-column prop="medicalPer" label="比率" width="60" align="center" />
        </el-table-column>
        <el-table-column label="公积金" align="center">
          <el-table-column prop="accumulationFundBase" label="基数" width="60" align="center" />
          <el-table-column prop="accumulationFundPer" label="比率" width="60" align="center" />
        </el-table-column>
        <el-table-column label="操作" align="left">
          <template slot-scope="scope">
            <el-button style="padding: 4px" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button style="padding: 4px" size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" :close-on-click-modal="false" :show-close="true" :before-close="beforeClose">
      <div style="display: flex;justify-content: space-around;align-items: center">
        <el-steps direction="vertical" :active="activeStep">
          <el-step v-for="item,index in steps" :key="index" :title="item" />
        </el-steps>
        <el-input v-for="value,key,index in row" v-show="activeStep === index" ref="inputRef" :key="index" v-model="row[key]" size="small" :placeholder="'请输入' + steps[index] + '...'" style="width: 200px" @keydown.enter.native="nextStep" />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button v-show="activeStep !== 0" size="small" @click="preStep">上一步</el-button>
        <el-button size="small" type="primary" @click="nextStep">{{ activeStep === steps.length - 1 ? '完成' : '下一步' }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { GetAll, Create, Delete, Update } from '@/api/Sal/SalSob'

export default {
  name: 'SalSob',
  data() {
    return {
      list: [],
      dialogVisible: false,
      steps: ['账套名称', '基本工资', '奖金', '午餐补助', '交通补助', '应发工资', '养老金基数', '养老金比率', '医疗保险基数', '医疗保险比率', '公积金基数', '公积金比率'],
      row: {
        name: null,
        basicSalary: null,
        bonus: null,
        lunchSalary: null,
        trafficSalary: null,
        allSalary: null,
        pensionBase: null,
        pensionPer: null,
        medicalBase: null,
        medicalPer: null,
        accumulationFundBase: null,
        accumulationFundPer: null
      },
      activeStep: 0,
      title: '添加工资账套'
    }
  },
  mounted() {
    this.getAll()
  },
  methods: {
    getAll() {
      GetAll().then((res) => {
        if (res) {
          this.list = res
        }
      })
    },
    handleCreate() {
      this.title = '创建工资账套'
      this.dialogVisible = true
      this.getFocus()
    },
    nextStep() {
      if (this.activeStep === this.steps.length - 1) {
        if (this.row.id) {
          Update(this.row).then((res) => {
            if (res) {
              this.getAll()
              this.dialogVisible = false
              this.initRow()
            }
          })
        } else {
          Create(this.row).then((res) => {
            if (res) {
              this.getAll()
              this.dialogVisible = false
              this.initRow()
            }
          })
        }
      } else {
        this.activeStep++
        this.getFocus()
      }
    },
    preStep() {
      if (this.activeStep !== 0) {
        this.activeStep--
        this.getFocus()
      }
    },
    getFocus() {
      // 在input输入框被渲染完毕后再获取焦点
      this.$nextTick(() => {
        // 引用原生DOM对象input的focus方法自动获得焦点
        this.$refs.inputRef[this.activeStep].focus()
      })
    },
    beforeClose(done) {
      this.initRow()
      done()
    },
    initRow() {
      this.activeStep = 0
      this.row = {
        name: null,
        basicSalary: null,
        bonus: null,
        lunchSalary: null,
        trafficSalary: null,
        allSalary: null,
        pensionBase: null,
        pensionPer: null,
        medicalBase: null,
        medicalPer: null,
        accumulationFundBase: null,
        accumulationFundPer: null
      }
    },
    handleRefresh() {

    },
    handleUpdate(row) {
      this.title = '编辑工资账套'
      this.row.name = row.name
      this.row.basicSalary = row.basicSalary
      this.row.bonus = row.bonus
      this.row.lunchSalary = row.lunchSalary
      this.row.trafficSalary = row.trafficSalary
      this.row.allSalary = row.allSalary
      this.row.pensionBase = row.pensionBase
      this.row.pensionPer = row.pensionPer
      this.row.medicalBase = row.medicalBase
      this.row.medicalPer = row.medicalPer
      this.row.accumulationFundBase = row.accumulationFundBase
      this.row.accumulationFundPer = row.accumulationFundPer
      this.row.createDate = row.createDate
      this.row.id = row.id
      this.dialogVisible = true
      this.getFocus()
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除【' + row.name + '】账套, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        Delete(row.id).then((res) => {
          if (res) {
            this.getAll()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
